package com.example.aqarapp.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import aqarapp.R;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.aqarapp.entity.BulbInfo;
import com.example.aqarapp.entity.CurtainInfo;
import com.example.aqarapp.entity.DeviceInfo;
import com.jaygoo.widget.RangeSeekBar;

public class CurtainControlDialog extends Dialog implements View.OnClickListener {
    private static final String TAG = "CURTAIN_CONTROL_DIALOG";
    private int mPosition;
    private CurtainInfo mCurtainInfo;
    private Context mContext;
    private Point mWindowSize = new Point();
    private RangeSeekBar mRangeSeekBar;
    private ImageButton mOpenCurtainIb;
    private ImageButton mPauseCurtainIb;
    private ImageButton mCloseCurtainIb;
    private DeviceInfo mDeviceInfo;
    private BaseQuickAdapter<DeviceInfo, ?> mAdapter;

    public CurtainControlDialog(@NonNull Context context, DeviceInfo deviceInfo, @NonNull BaseQuickAdapter<DeviceInfo, ?> adapter, int position) {
        super(context);
        mContext = context;
        mDeviceInfo =deviceInfo;
        mCurtainInfo = (CurtainInfo) deviceInfo.getBaseDeviceInfo();
        mAdapter = adapter;
        mPosition = position;
    }

    public CurtainControlDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected CurtainControlDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_curtain_inf_dialog);
        WindowManager windowManager = ((Activity) mContext).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        display.getSize(mWindowSize);
        lp.width = mWindowSize.x * 4 / 5;// 设置dialog宽度为屏幕的4/5
        getWindow().setAttributes(lp);
        setCanceledOnTouchOutside(true);//点击外部Dialog消失
        initCurtainView();
    }

    private void initCurtainView() {
        mRangeSeekBar = findViewById(R.id.curtain_open_percentage_sb);
        mOpenCurtainIb = findViewById(R.id.curtain_open_ib);
        mPauseCurtainIb = findViewById(R.id.curtain_pause_ib);
        mCloseCurtainIb = findViewById(R.id.curtain_close_ib);
        mOpenCurtainIb.setOnClickListener(this);
        mPauseCurtainIb.setOnClickListener(this);
        mCloseCurtainIb.setOnClickListener(this);
        mRangeSeekBar.setEnabled(false);
        mRangeSeekBar.setRange(0f, 100f);
        if(((CurtainInfo)mDeviceInfo.getBaseDeviceInfo()).getCurtainStatus().equals("1")){
            mRangeSeekBar.setProgress(0f, 100f);
        }else if(((CurtainInfo)mDeviceInfo.getBaseDeviceInfo()).getCurtainStatus().equals("0")){
            mRangeSeekBar.setProgress(50f, 50f);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.curtain_open_ib:
                //todo 远程打开窗帘，如果失败下面代码不执行
                mRangeSeekBar.setProgress(0f, 100f);
                ((CurtainInfo)mDeviceInfo.getBaseDeviceInfo()).setCurtainStatus("1");
                //2.同步cardView UI
                mAdapter.removeAt(mPosition);
                mAdapter.addData(mPosition, mDeviceInfo);
                mAdapter.notifyItemChanged(mPosition);
                break;
            case R.id.curtain_pause_ib:
                //todo 暂停操作
                Toast.makeText(mContext, "暂不支持暂停操作", Toast.LENGTH_SHORT).show();
                break;
            case R.id.curtain_close_ib:
                //todo 远程关闭窗帘，如果失败下面代码不执行
                mRangeSeekBar.setProgress(50f, 50f);
                ((CurtainInfo)mDeviceInfo.getBaseDeviceInfo()).setCurtainStatus("0");
                mAdapter.removeAt(mPosition);
                mAdapter.addData(mPosition,mDeviceInfo);
                mAdapter.notifyItemChanged(mPosition);
                break;
        }
    }
}
